package com.briup.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.briup.domain.User;
import com.briup.domain.extend.UserExtend;
import com.briup.exception.ServiceException;
import com.briup.resuit.ResultCode;
import com.briup.service.UserService;
import com.briup.mapper.UserMapper;
import com.briup.utils.JwtUtil;
import com.briup.utils.MD5Util;
import com.briup.vo.loginParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 白宇杰
* @description 针对表【cms_user】的数据库操作Service实现
* @createDate 2024-11-20 09:30:53
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
@Autowired
UserMapper userMapper;
    @Override
    public void saveUser(User user) {
        String password = user.getPassword();
        String username = user.getUsername();
        if (password==null || username==null) {
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        username=username.trim();
        password=password.trim();
        if (username.isEmpty() || password.isEmpty()) {
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        user.setUsername(username);
        user.setPassword(password);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper();
        lqw.eq(User::getUsername,username);
        User user1=baseMapper.selectOne(lqw);
        if (user1!=null) {
            throw new ServiceException(ResultCode.FENCE_HOUSE_IS_EXIST);
        }
        user.setRegisterTime(LocalDateTime.now());
        baseMapper.insert(user);
    }

    @Override
    public void setVip(Long id) {
        User user = baseMapper.selectById(id);
        if (user==null) {
            throw new ServiceException(ResultCode.USER_IS_NOT_EXIST);
        }
        LocalDateTime expiresTime;
        if (user.getIsVip()==1){
            expiresTime=user.getExpiresTime().plusMonths(1);
        }else {
            user.setIsVip(1);
            expiresTime=LocalDateTime.now().plusMonths(1);
        }
        user.setExpiresTime(expiresTime);
        baseMapper.updateById(user);
    }
    @Override
    public IPage<UserExtend> queryAll(Integer pageNum,Integer pageSize,
                                     String username, String status, Integer roleId, Integer isVip) {
        IPage<UserExtend> iPage=new Page<>(pageNum,pageSize);
        return userMapper.queryAll(iPage,username,status,roleId,isVip);
    }
    @Override
    public String login(loginParam param) {
        String username = param.getUsername();
        String password = MD5Util.MD5(param.getPassword());
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
        lqw.eq(User::getUsername, username).eq(User::getPassword, password);
        User user = userMapper.selectOne(lqw);
        Map<String,Object> claims=new HashMap<>();
        claims.put("userId",user.getId());
        claims.put("username",user.getUsername());
        String s = JwtUtil.generateJwt(claims);
        return s;
    }
}




